package com.example.pinkcat.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.adapter.VP_viewpager_adapter;
import com.example.pinkcat.activity.adapter.V_viewpager_adapter;
import com.example.pinkcat.activity.api.getData.GetData;

import java.util.ArrayList;
import java.util.Objects;

public class VideoPinkcat extends AppCompatActivity {
    static boolean mediaIsVisible;//媒体控件显示位
    static boolean isplaying=false;//是否在播放视频
    static boolean isLandScape=false;//是否横屏
    int videoCurrent=0;//视频进度

    View toplayout;
    Uri video_uri=Uri.parse("http://49.234.151.152:8089/getQQ0_1_Web/tjym_video/202104211702_12.mp4");//视频地址
    VideoView video;//视频控件
    View bar_wait;//视频等待控件
    ProgressBar bar_progress;//视频进度条控件

    View mediacontrller;//媒体播放控制
    SeekBar seekBar_media;//媒体控制进度条
    TextView progress_txt;//文本进度
    ImageView startvideobtn;//播放按钮
    View video_bake;//视频页面返回按键
    View full_video_btn;//全屏键

    ViewPager viewPager;//viewpager组件
    PagerTabStrip pagerTab;  //PagerTabStrip组件
    ArrayList<View> views=new ArrayList<>();

    String intent_v_id;//视频id
    String intent_v_link;//视频连接
    String intent_up_id;//视频用户id
    String intent_v_labs;//视频用户id
    Context context=this;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_video_pinkcat);

        //接收视频id——视频link
        Intent intent=getIntent();
        if (intent!=null){
            intent_v_link=intent.getStringExtra("v_link");
            String idssss=intent.getStringExtra("v_id");
            intent_up_id=intent.getStringExtra("up_id");
            intent_v_labs=intent.getStringExtra("v_labs");
            if (intent_v_link!=null){
                video_uri=Uri.parse(intent_v_link);
            }
            if (idssss!=null){
                intent_v_id=idssss;
            }
        }

        video=findViewById(R.id.video);//视频控件
        bar_wait=findViewById(R.id.progressBar);//视频等待控件
        bar_progress=findViewById(R.id.seekBar);//视频进度条控件

        mediacontrller=findViewById(R.id.mediacontrller);//媒体播放控制
        seekBar_media=findViewById(R.id.seekBar2);//媒体控制进度条
        progress_txt=findViewById(R.id.time_video_txt);//文本进度
        startvideobtn=findViewById(R.id.start_video_play);//播放按钮
        video_bake=findViewById(R.id.video_back);//视频页面返回按键
        full_video_btn=findViewById(R.id.full_video_btn);//全屏键
        viewPager=findViewById(R.id.video_viewpager);//viewpager组件
        pagerTab=findViewById(R.id.video_viewpager_Table);//pagertablestrip组件

        toplayout=findViewById(R.id.toplayout);  toplayout.setVisibility(View.INVISIBLE);


        mediaIsVisible=false;//媒体控件显示位false
        bar_wait.setVisibility(View.VISIBLE);//等待bar显示
        mediacontrller.setVisibility(View.INVISIBLE);//默认媒体控制消失
        bar_progress.setVisibility(View.VISIBLE);//默认底部进度条显示
        video_bake.setVisibility(View.INVISIBLE);//默认返回键消失
        startvideobtn.setVisibility(View.INVISIBLE);//播放键消失
        full_video_btn.setVisibility(View.INVISIBLE);//播放键消失

        //video_uri=Uri.parse("http://49.234.151.152:8089/getQQ0_1_Web/tjym_video/202104211702_12.mp4");//视频地址
        video.setVideoURI(video_uri);//设置视频地址
        video.requestFocus();//视频focus

        listen();
        //初始化viewpager
        SetViews();
        GetData.addplaynum(context,intent_v_id);//增加视频播放量
    }


    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("resume");
        bar_wait.setVisibility(View.VISIBLE);//等待bar显示
        video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                bar_wait.setVisibility(View.INVISIBLE);//等待bar消失
                video.seekTo(videoCurrent);
                video.start();
                isplaying=true;
                startvideobtn.setImageResource(R.mipmap.pause_white);
                mp.setLooping(false);
            }
        });//视频准备好了进行操作
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("pause");

        video.pause();
        videoCurrent=video.getCurrentPosition();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {//获取返回值
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==115 && resultCode ==115){
            videoCurrent=data.getIntExtra("videoCurrent",0);
        }
    }

    public void SetViews(){//设置vewpager页面
        views.add(getLayoutInflater().inflate(R.layout.video_viewpager_page1, null));
        views.add(getLayoutInflater().inflate(R.layout.video_viewpager_page2, null));

        VP_viewpager_adapter video_viewpager_adapter=new VP_viewpager_adapter(new String[]{"简介", "评论"},views,context,intent_up_id,intent_v_id,intent_v_labs,video);
        viewPager.setAdapter(video_viewpager_adapter);
        pagerTab.setTabIndicatorColor(getResources().getColor(R.color.pink));//标题栏指示器的颜色
        //pagerTab.setTextColor(getResources().getColor(R.color.pink));  //设置标题字体颜色
        pagerTab.setTextSpacing(20);
        pagerTab.setTextSize(1, 16);  //设置标题字体大小
        pagerTab.setDrawFullUnderline(false);//下横线
        viewPager.setCurrentItem(0);//设定默认选中的
    }

    void listen(){//监听事件
        video_bake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });//返回按钮监控
        full_video_btn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,VideoPinkcat_full.class);
                videoCurrent=video.getCurrentPosition();
                intent.putExtra("videoCurrent",videoCurrent);//视频进度
                intent.putExtra("v_link",intent_v_link);//视频
                startActivityForResult(intent,115);
            }
        });//进入全屏键监控
        findViewById(R.id.relativeLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!mediaIsVisible){
                    mediacontrller.setVisibility(View.VISIBLE);
                    bar_progress.setVisibility(View.INVISIBLE);
                    video_bake.setVisibility(View.VISIBLE);
                    startvideobtn.setVisibility(View.VISIBLE);
                    full_video_btn.setVisibility(View.VISIBLE);
                    toplayout.setVisibility(View.VISIBLE);
                    mediaIsVisible=true;
                }
                else if (mediaIsVisible){
                    mediacontrller.setVisibility(View.INVISIBLE);
                    bar_progress.setVisibility(View.VISIBLE);
                    video_bake.setVisibility(View.INVISIBLE);
                    startvideobtn.setVisibility(View.INVISIBLE);
                    full_video_btn.setVisibility(View.INVISIBLE);
                    toplayout.setVisibility(View.INVISIBLE);
                    mediaIsVisible=false;
                }
            }
        });//点击视频出现媒体控制以及返回按钮，底部进度条消失

        startvideobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isplaying){
                    isplaying=false;
                    startvideobtn.setImageResource(R.mipmap.start_white);
                    video.pause();
                }
                else{
                    isplaying=true;
                    startvideobtn.setImageResource(R.mipmap.pause_white);
                    video.start();
                }
            }
        });//监控用户播放暂停按钮

        new Thread(){
            @SuppressLint("SetTextI18n")
            @Override
            public void run(){
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    double time=(video.getCurrentPosition()/(video.getDuration()+0.0))*1000;
                    int timeint= (int) time;
                    bar_progress.setProgress(timeint);
                    seekBar_media.setProgress(timeint);
                }
            }
        }.start();//线程控制底部进度条,和媒体进度条与视频播放进度一致

        seekBar_media.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){
                    double time=(seekBar.getProgress()/(1000+0.0))*video.getDuration();
                    int timeint=(int) time;
                    video.seekTo(timeint);
                    //video.start();
                    isplaying=true;
                }
                int hour_now=(video.getCurrentPosition()/1000)/3600;
                int min_now=(video.getCurrentPosition()/1000)/60-(60*hour_now);
                int seconds_now=(video.getCurrentPosition()/1000)-(60*min_now);
                int hour_all=(video.getDuration()/1000)/3600;
                int min_all=(video.getDuration()/1000)/60-(60*hour_all);
                int seconds_all=(video.getDuration()/1000)-(60*min_all);
                progress_txt.setText(hour_now+":"+min_now+":"+seconds_now+"/"+hour_all+":"+min_all+":"+seconds_all);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });//调整视频进度
    }
}